package edu.api.date_api;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Demo1 {


    public static void main(String[] args) throws ParseException {
        test5();
    }


    /**
     * 手动指定格式
     *
     */
    public static void test5(){
        String time = "2022年05月01日 18:22:59";
        DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = null;
        try {
            //字符串类型转换成日期类型
            date = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("date = " + date);
        //将日期类型转换成字符串类型
        Calendar calendar = Calendar.getInstance();
        calendar.set(2020,5,16,20,56,59);
        Date time1 = calendar.getTime();
        String s = dateFormat.format(time1);
        System.out.println("s = " + s);

    }



    /**
     * 日期的格式化问题：
     * 字符串类型日期 2022-6-13   转换成 Date
     * Date转换成  字符串格式
     */
    public static void test4() throws ParseException {
        //日期格式化
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);
        DateFormat dateFormat2 = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
        DateFormat dateFormat3 = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINA);
        DateFormat dateFormat4 = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA);
        //时间的格式化
        DateFormat.getTimeInstance(DateFormat.LONG,Locale.CHINA);
        //日期 + 时间格式化
        DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG,Locale.CHINA);
        //将日期类型转换成字符串类型
        String s = dateFormat.format(new Date());
        System.out.println("s = " + s);
        s = dateFormat2.format(new Date());
        System.out.println("s = " + s);
        s = dateFormat3.format(new Date());
        System.out.println("s = " + s);
        s = dateFormat4.format(new Date());
        System.out.println("s = " + s);
        //将字符串类型转换成日期类型
        Date date = dateFormat4.parse(s);
        System.out.println("date = " + date);

    }




    /**
     * 日历设置
     */
    public static void test3(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(2021,10,1);
        Date time = calendar.getTime();
        System.out.println("time = " + time);
        //根据某个字段修改日历信息
        calendar.set(Calendar.YEAR,2025);
        Date calendarTime = calendar.getTime();
        System.out.println("calendarTime = " + calendarTime);

    }

    /**
     * Calendar :日历
     */
    public static void test2(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(2021,6,3);
        System.out.println("calendar = " + calendar);
        //获取时间
        Date time = calendar.getTime();
        System.out.println("time = " + time);
        //获取本周的第一天，会根据时区的不同，结果不同
        int firstDayOfWeek = calendar.getFirstDayOfWeek();
        System.out.println("firstDayOfWeek = " + firstDayOfWeek);
        //获取当前周年
        int weekYear = calendar.getWeekYear();
        System.out.println("weekYear = " + weekYear);
        //获取周年有多少周
        int weeksInWeekYear = calendar.getWeeksInWeekYear();
        System.out.println("weeksInWeekYear = " + weeksInWeekYear);
        //指定字段获取日历中的数据
        //获取星期几
        int i = calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println("i = " + i);
        int am_pm = calendar.get(Calendar.AM_PM);
        System.out.println("am_pm = " + am_pm);
        int year = calendar.get(Calendar.YEAR);
        System.out.println("year = " + year);
        int month = calendar.get(Calendar.MONTH);
        System.out.println("month = " + month);
        int date = calendar.get(Calendar.DATE);
        System.out.println("date = " + date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println("day = " + day);
        int day_of_year = calendar.get(Calendar.DAY_OF_YEAR);
        System.out.println("day_of_year = " + day_of_year);
        // .......
    }


    /**
     * Date的快速使用
     */
    public static void test1(){
        //获取当前系统时间
        Date date = new Date();
        System.out.println("date = " + date);
        //获取30分钟之后的时间
        Date date2 = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
        System.out.println("date2 = " + date2);
        //获取日期的毫秒值
        long time = date.getTime();
        System.out.println("time = " + time);
        //compareTo
        int i = date.compareTo(date2);
        System.out.println("i = " + i);
        //after
        boolean after = date.after(date2);
        System.out.println("after = " + after);
        //修改日期
        date.setTime(System.currentTimeMillis() + 35 * 60 * 1000);
        //before
        boolean before = date.before(date2);
        System.out.println("before = " + before);
        String s = date.toString();
        System.out.println("s = " + s);
    }


}
